﻿using System;
using System.Windows;

using GroupChallenge.GroupChallengeSvc;

namespace GroupChallenge
{
    public partial class QuestionsViewModel
    {
        private Question _CurrentQuestion = new Question();
        public Question CurrentQuestion
        {
            get { return _CurrentQuestion; }
            set { _CurrentQuestion = value; }
        }

        internal void Clear()
        {
            CurrentQuestion.QuestionText = string.Empty;
            CurrentQuestion.QuestionTypeId = QuestionType.MultipleChoice;
            CurrentQuestion.CorrectAnswerId = -1;

            CurrentQuestion.Answer1 = string.Empty;
            CurrentQuestion.Answer2 = string.Empty;
            CurrentQuestion.Answer3 = string.Empty;
            CurrentQuestion.Answer4 = string.Empty;

            CurrentQuestion.SubmittedByPlayerNickName = string.Empty;
        }

        internal void SubmitQuestion()
        {
            string err = ValidateQuestion();

            if (!string.IsNullOrEmpty(err))
            {
                MessageBox.Show(err, "Submit Question Error", MessageBoxButton.OK);
            }
            else
            {
                InsertUpdateQuestion();
            }
        }

        private string ValidateQuestion()
        {
            string ret = string.Empty;

            if (null == CurrentQuestion)
                ret = "Missing current question.";
            else if (string.IsNullOrEmpty(MainApp.ViewModel.NickName))
                ret = "Missing current nick name.";
            else if (CurrentQuestion.CorrectAnswerId < 1)
                ret = "Missing correct answer selection.";

            return ret;
        }

        internal void InsertUpdateQuestion()
        {
            MainApp.ViewModel.Client.SubmitQuestionCompleted += new EventHandler<GroupChallengeSvc.SubmitQuestionCompletedEventArgs>(CompletedSubmitQuestion);
            MainApp.ViewModel.Client.SubmitQuestionAsync(MainApp.ViewModel.NickName, CurrentQuestion);
        }

        private void CompletedSubmitQuestion(object sender, GroupChallengeSvc.SubmitQuestionCompletedEventArgs e)
        {
            if (null != e.Error)
            {
                MessageBox.Show("Unable to submit the question.\r\n\r\nReason: " + e.Error.Message, "Submit Question Error", MessageBoxButton.OK);
            }
            else
            {
                FriendlyResult friendlyResult = e.Result as FriendlyResult;
                if (null != friendlyResult)
                {
                    if (friendlyResult.ResultId > 0)         // Success
                    {
                        MessageBox.Show(friendlyResult.FriendlyText, "Success", MessageBoxButton.OK);
                    }
                    else
                    {
                        MessageBox.Show(friendlyResult.FriendlyText, "Failure", MessageBoxButton.OK);
                    }
                }
            }

            // Unregister from the client that remains in the App.
            MainApp.ViewModel.Client.SubmitQuestionCompleted -= new EventHandler<GroupChallengeSvc.SubmitQuestionCompletedEventArgs>(CompletedSubmitQuestion);
        }
    }
}
